/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.iquest.explorer;


import java.awt.Component;
import java.awt.event.ActionListener;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import org.openide.explorer.propertysheet.ExPropertyEditor;
import org.openide.explorer.propertysheet.InplaceEditor;
import org.openide.explorer.propertysheet.PropertyEnv;
import org.openide.explorer.propertysheet.PropertyModel;


/**
 *
 * @author rejun001
 */
public class FormNumberPropertyEditor extends PropertyEditorSupport
        implements ExPropertyEditor , InplaceEditor.Factory
{

    private InplaceEditor ed = null;


    @Override
    public void attachEnv( PropertyEnv pe )
    {
        pe.registerInplaceEditorFactory( this );
    }


    @Override
    public InplaceEditor getInplaceEditor()
    {
        if ( ed == null )
        {
            ed = new Inplace();
        }
        return ed;

    }


    private static class Inplace implements InplaceEditor
    {

        private final JComboBox cbFormNumberPicker = new JComboBox();
        private PropertyEditor editor = null;


        @Override
        public void connect( PropertyEditor pe , PropertyEnv pe1 )
        {
            editor = pe;
            reset();
        }


        @Override
        public JComponent getComponent()
        {
            return cbFormNumberPicker;
        }


        public JComboBox getComboBox()
        {
            return (JComboBox) getComponent();
        }


        @Override
        public void clear()
        {
            // avoid memory leaks
            editor = null;
            model = null;
        }


        @Override
        public Object getValue()
        {
            return cbFormNumberPicker.getSelectedItem();
        }


        @Override
        public void setValue( Object o )
        {
            cbFormNumberPicker.setSelectedItem( o );
        }


        @Override
        public boolean supportsTextEntry()
        {
            return true;
        }


        @Override
        public void reset()
        {
            String s = (String) editor.getValue();
            if ( s != null )
            {
                cbFormNumberPicker.setSelectedItem( s );
            }
        }


        @Override
        public KeyStroke[] getKeyStrokes()
        {
            return new KeyStroke[ 0 ];
        }


        @Override
        public PropertyEditor getPropertyEditor()
        {
            return editor;
        }


        @Override
        public PropertyModel getPropertyModel()
        {
            return model;
        }

        private PropertyModel model;


        @Override
        public void setPropertyModel( PropertyModel propertyModel )
        {
            this.model = propertyModel;
        }


        @Override
        public void addActionListener( ActionListener al )
        {
            // no-op not needed
        }


        @Override
        public void removeActionListener( ActionListener al )
        {
            // no-op not needed
        }


        @Override
        public boolean isKnownComponent( Component cmpnt )
        {
            return cmpnt == cbFormNumberPicker || cbFormNumberPicker.isAncestorOf( cmpnt );
        }

    }

}
